YUI.add('progress-bar', function (Y) {
    /* Any frequently used shortcuts, strings and constants */
    var Lang = Y.Lang,
    Widget = Y.Widget,
    Node = Y.Node;

    /* ProgressBar class constructor */
    function ProgressBar(config) {
        ProgressBar.superclass.constructor.apply(this, arguments);
    }

    /* 
     * Required NAME static field, to identify the class and 
     * used as an event prefix, to generate class names etc. (set to the 
     * class name in camel case). 
     */
    ProgressBar.NAME = "ProgressBar";
	/*
	 * The attribute configuration for the ProgressBar widget. Attributes can be
	 * defined with default values, get/set functions and validator functions
	 * as with any other class extending Base.
	 */
	ProgressBar.ATTRS = {
	    // The minimum value for the ProgressBar.
	    progress : {
	        setter : function(val){
	        	if(!val)return '0%';
	        	if(Lang.isString(val)){
	        		if(val.indexOf('%') == -1){
	        			return val + "%";
	        		}else{
	        			return val;
	        		}
	        	}else if(Lang.isNumber(val)){
	        		return (Math.round(val * 10000))/100 + "%";
	        	}
	        }
	    },
	
	    // override default ("null"), required for focus()
	    tabIndex: {
	        value: 0
	    },
	
	    // The strings for the ProgressBar UI. This attribute is 
	    // defined by the base Widget class but has an empty value. The
	    // ProgressBar is simply providing a default value for the attribute.
	    strings: {
	        value: {
	            tooltip: "Press the arrow up/down keys for minor increments, page up/down for major increments.",
	            sure: "sure",
	            cancel: "cancel"
	        }
	    }
	};
	/* Static constant used to identify the classname applied to the ProgressBars value field */
	ProgressBar.C_CLASS = Y.ClassNameManager.getClassName(ProgressBar.NAME, "container");
	
	ProgressBar.COLOR_BAR_CLASS = Y.ClassNameManager.getClassName(ProgressBar.NAME, "color-bar");
	
	ProgressBar.TEXT_BAR_CLASS = Y.ClassNameManager.getClassName(ProgressBar.NAME, "text-bar");
	
	/* Static constants used to define the markup templates used to create ProgressBar DOM elements */
	ProgressBar.BAR_TEMPLATE = '<div class="' + ProgressBar.C_CLASS + '"><span class="'+ ProgressBar.COLOR_BAR_CLASS +'"></span></div><span class="'+ ProgressBar.TEXT_BAR_CLASS +'"></span>';
	
	/* 
	 * The HTML_PARSER static constant is used by the Widget base class to populate 
	 * the configuration for the ProgressBar instance from markup already on the page.
	 *
	 * The ProgressBar class attempts to set the value of the ProgressBar widget if it
	 * finds the appropriate input element on the page.
	 */
	ProgressBar.HTML_PARSER = {
//	    file: function (srcNode) {
//	        return srcNode.one('.' + ProgressBar.FILE_CLASS); 
//	    }
	};
	
	/* ProgressBar extends the base Widget class */
	Y.extend(ProgressBar, Widget, {
	
	    /*
	     * initializer is part of the lifecycle introduced by 
	     * the Widget class. It is invoked during construction,
	     * and can be used to setup instance specific state.
	     * 
	     * The ProgressBar class does not need to perform anything
	     * specific in this method, but it is left in as an example.
	     */
	    initializer: function() {
	        // Not doing anything special during initialization
	    	
	    },
	
	    /*
	     * destructor is part of the lifecycle introduced by 
	     * the Widget class. It is invoked during destruction,
	     * and can be used to cleanup instance specific state.
	     * 
	     * The ProgressBar cleans up any node references it's holding
	     * onto. The Widget classes destructor will purge the 
	     * widget's bounding box of event listeners, so ProgressBar 
	     * only needs to clean up listeners it attaches outside of 
	     * the bounding box.
	     */
	    destructor : function() {
	    	
	    },
	    
	    /*
	     * renderUI is part of the lifecycle introduced by the
	     * Widget class. Widget's renderer method invokes:
	     *
	     *     renderUI()
	     *     bindUI()
	     *     syncUI()
	     *
	     * renderUI is intended to be used by the Widget subclass
	     * to create or insert new elements into the DOM. 
	     *
	     * For ProgressBar the method adds the input (if it's not already 
	     * present in the markup), and creates the inc/dec buttons
	     */
	    renderUI : function() {
	    	var contentBox = this.get("contentBox");
	    	var b = Node.create(ProgressBar.BAR_TEMPLATE);
	    	contentBox.appendChild(b);
	    },
	
	    /*
	     * bindUI is intended to be used by the Widget subclass 
	     * to bind any event listeners which will drive the Widget UI.
	     * 
	     * It will generally bind event listeners for attribute change
	     * events, to update the state of the rendered UI in response 
	     * to attribute value changes, and also attach any DOM events,
	     * to activate the UI.
	     * 
	     * For ProgressBar, the method:
	     *
	     * - Sets up the attribute change listener for the "value" attribute
	     *
	     * - Binds key listeners for the arrow/page keys
	     * - Binds mouseup/down listeners on the boundingBox, document respectively.
	     * - Binds a simple change listener on the input box.
	     */
	    bindUI : function() {
	    	this.after("progressChange", this._afterprogressChange);
	    },
	
	    /*
	     * syncUI is intended to be used by the Widget subclass to
	     * update the UI to reflect the current state of the widget.
	     * 
	     * For ProgressBar, the method sets the value of the input field,
	     * to match the current state of the value attribute.
	     */
	    syncUI : function() {
	    	var contentBox = this.get('contentBox');
	    	var colorBar = contentBox.one('.' + ProgressBar.COLOR_BAR_CLASS);
	    	var textBar = contentBox.one('.' + ProgressBar.TEXT_BAR_CLASS);
	    	var percent = this.get('progress');
	    	if(!percent)percent = '0%';
	    	colorBar.setStyle('width',percent);
	    	textBar.setContent(percent);
	    },
	    
	    _afterprogressChange : function(e){
	    	this.syncUI();
	    }
	});
    Y.namespace("M3958").ProgressBar = ProgressBar;

},'0.0.1',{requires:['widget']});